﻿'use strict';
QuotingToolApp
.factory('QuoteData', function ($resource, QUOTINGTOOL_CONFIG) {
    return $resource(QUOTINGTOOL_CONFIG.baseUrl + '/api/orcl/quote/:id/:lineid', null, {
        update: {
            method: 'PUT'
        },       
        delete: {
            method: 'DELETE'
        }        
    });
})
.factory('QuoteServices', ['$resource', 'QUOTINGTOOL_CONFIG',
    function ($resource, QUOTINGTOOL_CONFIG) {

        return {
            //    getquotecustomers

            //        Parameters : {headerid}/{customertype}

            //    url: http://quotingtool/api/orcl/getquotecustomers/170/SOLD_TO  (FOR EXAMPLE : 170,SOLD_TO)
           


            getQuoteCustomers: function (searchParams) {
                //search oracle customers by customer name or part of the name
                return $resource(QUOTINGTOOL_CONFIG.baseUrl + '/api/orcl/getquotecustomers/:headerid/:customertype', { headerid: '@headerid', customertype: '@customertype' })
                                .query({ headerid: searchParams.headerId, customertype: searchParams.customerType });
            },

            CreateQuote: function (searchParams) {
                //search oracle customers by customer name or part of the name
                return $resource(QUOTINGTOOL_CONFIG.baseUrl + '/api/sf/createquote/:headerid', { headerid: '@headerid'})
                                .query({ headerid: searchParams.headerId});
            },
            getBillToShipToCustomers: function (searchParams) {

                //api/orcl/getcustomersbysoldto/{soldtopartyid}/{operatingunitid}
                return $resource(QUOTINGTOOL_CONFIG.baseUrl + '/api/orcl/getcustomersbysoldto/:soldtopartyid/:operatingunitid', { soldtopartyid: '@soldtopartyid', operatingunitid: '@operatingunitid' })
                               .query({ soldtopartyid: searchParams.SOLD_TO_PARTY_ID, operatingunitid: searchParams.OPERATING_UNIT_ID });

            },
            
            getBillToShipToAddress: function(searchParams){

                // api/orcl/getbillshipaddress/{custaccountid}/{siteusetype}/{operatingunitid}
                return $resource(QUOTINGTOOL_CONFIG.baseUrl + '/api/orcl/getbillshipaddress/:custaccountid/:siteusetype/:operatingunitid',
                    { custaccountid: '@custaccountid', siteusetype: '@siteusetype', operatingunitid: '@operatingunitid' })
                    .query({ custaccountid: searchParams.custAccountId, siteusetype: searchParams.siteUseType, operatingunitid: searchParams.OPERATING_UNIT_ID });
            },

            getOperatingUnits: function(){
                return $resource(QUOTINGTOOL_CONFIG.baseUrl + '/api/orcl/getoperatingunits',{}).query({});
            },

            getMaintenanceItems: function (topDivision) {

                return $resource(QUOTINGTOOL_CONFIG.baseUrl + '/api/orcl/getmaintenanceitems/:topdivision',
                  { topdivision: '@topdivision' })
                    //.query({ topdivision: '4100' });
                      .query({ topdivision: topDivision });

                //Getmaintenanceitems:

                //    api/orcl/getmaintenanceitems/{topdivision}

                //params :
                //    topdivision

            },

            revertDiscount: function (searchParams) {
                return $resource(QUOTINGTOOL_CONFIG.baseUrl + '/api/orcl/quote/revertdiscount/:headerid', { headerid: '@headerid' })
                    .get({ headerid: searchParams.headerId });
            },

            setDiscount: function (searchParams) {
                //return $resource(QUOTINGTOOL_CONFIG.baseUrl + '/api/orcl/quote/setdiscount/:headerid', { headerid: '@headerid' }).query({ headerid: searchParams.headerId });
               
                var resource = $resource(QUOTINGTOOL_CONFIG.baseUrl + '/api/orcl/quote/setdiscount/:headerid/:OneLineDiscountType/:OneLineDiscount/:PrePaidIndication/:MultiYearIncentive',
                   {
                       headerid: '@headerid',
                       OneLineDiscountType: '@OneLineDiscountType',
                       OneLineDiscount: '@OneLineDiscount',
                       PrePaidIndication: '@PrePaidIndication',
                       MultiYearIncentive: '@MultiYearIncentive'

                   });
                
                return resource.get({
                    headerid: searchParams.headerid,
                    OneLineDiscountType: searchParams.OneLineDiscountType,
                    OneLineDiscount: searchParams.OneLineDiscount,
                    PrePaidIndication: searchParams.PrePaidIndication,
                    MultiYearIncentive: searchParams.MultiYearIncentive                    
                });                
               
            },

            copyQuote: function (searchParams) {
                //api/orcl/quote/copyquote/{headerid}
                return $resource(QUOTINGTOOL_CONFIG.baseUrl + '/api/orcl/quote/copyquote/:headerid', { headerid: '@headerid' })
                  .get({ headerid: searchParams.headerId });
            }           

        }
}])
.factory('QuoteSFDCData', ['$resource', 'QUOTINGTOOL_CONFIG',
    function ($resource, QUOTINGTOOL_CONFIG) {
        return {

            searchSFDC: function (searchParams) {
                //api/SF/searchopportunity/{oppnumber}/{oppname}/{topdivision}/{oppowner}/{sfaccount}

                var resource = $resource(QUOTINGTOOL_CONFIG.baseUrl + '/api/SF/searchopportunity/:oppnumber/:oppname/:topdivision/:oppowner/:sfaccount',
                    { oppnumber: '@oppnumber', oppname: '@oppname', oppowner: '@oppowner', topdivision: '@topdivision', sfaccount: 'sfaccount' });

                return resource.query({
                    oppnumber: searchParams.oppNumber,
                    oppname: searchParams.oppName,
                    oppowner: searchParams.oppOwner,
                    sfaccount: searchParams.accountID,
                    topdivision: searchParams.topDivision
                });
            }
        };

}]);
